'''
UI操作相关
'''
import requests
from selenium import webdriver
from setting.setting import get_conf
import re
import os
import zipfile


# 解压zip
def unzip(file, outpath):
    print(file)
    with zipfile.ZipFile(file) as zip_fp:
        for names in zip_fp.namelist():
            zip_fp.extract(names, path=outpath)
        zip_fp.close()


# 根据配置获取驱动
def get_driver():
    url = get_conf('CHROME', 'driver_source_adress')
    version = get_conf('CHROME', 'driver_version')
    save_path = get_conf('CHROME', 'driver_path')
    res = requests.get(url=url)
    match_res = re.findall('<a href="/(.*?)">(.*?)/</a>', res.text)
    version_url_list = []
    for v in match_res:
        vs = v[1].split('.')[0]
        if vs.isdigit() and (version in vs):
            version_url_list.append(v[0])
    # 这里取最大版本下载
    if version_url_list:
        url = url + version_url_list[-1].split('/')[-2] + '/' + 'chromedriver_win32.zip'
        print('正在下载谷歌{}驱动版本……\n下载地址\n{}'.format(url.split('/')[-2], url))
        res = requests.get(url)
        filepath = os.path.dirname(save_path)
        file_name = 'driver_win{}.zip'.format(version)
        with open(os.path.join(filepath, file_name), 'wb') as fp:
            fp.write(res.content)
        print('驱动下载成功')
        # 解压文件
        unzip(os.path.join(filepath, file_name), save_path)
    else:
        print('未找到驱动版本')

    # print(match_res)


# 打开页面
def open_page(url):
    '''
    :param url: 地址
    :return: driver obj
    '''
    start_path = get_conf('CHROME', 'driver_path') + 'chromedriver.exe'

    for _ in range(int(get_conf('CHROME', 'error_count'))):
        try:
            driver = webdriver.Chrome(executable_path=start_path)
            driver.maximize_window()
            driver.get(url)
            return driver
        except Exception as e:
            print('启动浏览器发生错误，正在尝试更新驱动重试中……\n错误信息：{}'.format(e))
            get_driver()
            continue


if __name__ == '__main__':
    get_driver()
    driver = open_page('https://wms-test.gwm.com.cn/#/l/vin_wave_generate_page/new')
    driver.maximize_window()
    driver.find_element_by_xpath('//input[@autofocus="autofocus"]').send_keys('hh')
    driver.find_element_by_xpath('//input[@type="password"]').send_keys('123456')
    driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/form/div[3]/button').click()
    import time
    time.sleep(2)
    driver.find_element_by_xpath('//*[contains(text(),"自提出库单")]').click()
    while True:
        for i in range(1, 11):
            driver.find_element_by_xpath('//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div/div/div[2]/div[3]/div/div[3]/div[2]/div[4]/div[2]/table/tbody/tr['+str(i)+']/td[2]/div/label/span/span').click()
            # time.sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div/div/div[2]/div[3]/div/div[1]/span/button/span').click()
        time.sleep(2)
        driver.find_element_by_xpath('/html/body/div[13]/div/div[3]/div/button[2]/span').click()
        time.sleep(4)
